package com.jic.product.controller.manage;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.product.common.util.BeanUtil;
import com.jic.product.entity.ProductGuarantee;
import com.jic.product.manage.request.ProductGuaranteeRequest;
import com.jic.product.manage.response.ProductGuarnteeResponse;
import com.jic.product.service.ProductGuaranteeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by WS on 2020/1/13  9: 26
 * 商品保障
 */
@RestController
@RequestMapping("/productGuarantee")
@Api(value = "商品保障controller", tags = {"商品保障"})
@Slf4j
public class ProductGuaranteeController {

    @Autowired
    private ProductGuaranteeService productGuaranteeService;

    /*
     * 分页查询
     * */
    @ApiOperation(value="获取所有商品保障信息")
    @ResponseBody
    @PostMapping(value = "/queryPage")
    public RestResult<PageResult<ProductGuarnteeResponse>> queryPage(@RequestBody  ProductGuaranteeRequest productGuaranteeRequest) {

        Page pageQuery = new Page();
        pageQuery.setLimit(productGuaranteeRequest.getLimit());
        pageQuery.setStart(productGuaranteeRequest.getStart());

        ProductGuarantee productGuarantee = BeanUtil.copy(productGuaranteeRequest, ProductGuarantee.class);

        PageResult<ProductGuarantee> result= productGuaranteeService.queryPage(productGuarantee,pageQuery);
        List<ProductGuarnteeResponse> productGuarnteeResponseList = result.getRows().stream().map(temp -> {
            ProductGuarnteeResponse productGuarnteeResponse = BeanUtil.copy(temp, ProductGuarnteeResponse.class);
            return productGuarnteeResponse;
        }).filter(item -> item != null).collect(Collectors.toList());

        PageResult<ProductGuarnteeResponse> pageResult = new PageResult<>();
        pageResult.setRows(productGuarnteeResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }


    /*
     *  商品保障新增
     *
     * */
    @ApiOperation(value="商品保障新增")
    @ResponseBody
    @PostMapping(value = "/insertProductGuarantee")
    public RestResult insertProductBrand(@RequestBody ProductGuaranteeRequest productGuaranteeRequest) {
        RestResult restResult = new RestResult<>();
        try {
            ProductGuarantee productGuarantee = BeanUtil.copy(productGuaranteeRequest, ProductGuarantee.class);
            productGuarantee.setDeptId(productGuaranteeRequest.getAdminOperate().getDeptId());
            productGuarantee.setCreator(productGuaranteeRequest.getAdminOperate().getOperator());
            long flag = productGuaranteeService.insert(productGuarantee);
            if(flag == 1){
                restResult=RestResult.success(restResult);
            }
        }catch (Exception e) {
            log.error("9999",e.getLocalizedMessage());
            restResult=RestResult.error("9999",e.getLocalizedMessage());
        }
        return restResult;
    }

    /*
     *  商品保障修改
     *
     * */
    @ApiOperation(value="商品保障修改")
    @ResponseBody
    @PostMapping(value = "/updateProductGuarantee")
    public RestResult updateProductGuarantee(@RequestBody ProductGuaranteeRequest productGuaranteeRequest) {
        RestResult restResult = new RestResult<>();

        try {
            ProductGuarantee productGuarantee = BeanUtil.copy(productGuaranteeRequest, ProductGuarantee.class);
            productGuarantee.setUpdator(productGuaranteeRequest.getAdminOperate().getOperator());
            productGuarantee.setUpdaterName(productGuarantee.getAdminOperate().getOperateUsername());
            long flag = productGuaranteeService.update(productGuarantee);

            if(flag == 1){
                restResult=RestResult.success(restResult);
            }
        }catch (Exception e) {
            log.error("9999",e.getLocalizedMessage());
            restResult=RestResult.error("9999",e.getLocalizedMessage());
        }
        return restResult;
    }

    /*
     *  商品保障删除
     *
     * */
    @ApiOperation(value="商品保障删除")
    @ResponseBody
    @PostMapping(value = "/deleteProductGuarantee")
    public RestResult deleteProductGuarantee(@RequestBody ProductGuaranteeRequest productGuaranteeRequest) {
        RestResult restResult = new RestResult<>();
        try {
            long flag = productGuaranteeService.delete(productGuaranteeRequest.getId());
            if(flag == 1){
                restResult=RestResult.success(restResult);
            }
        }catch (Exception e) {
            log.error("9999",e.getLocalizedMessage());
            restResult=RestResult.error("9999",e.getLocalizedMessage());
        }
        return restResult;
    }


    /*
     *  商品保障条件查询
     *
     * */
    @ApiOperation(value="商品保障条件查询")
    @ResponseBody
    @PostMapping(value = "/queryProductGuarantee")
    public RestResult query(@RequestBody ProductGuaranteeRequest productGuaranteeRequest) {
        productGuaranteeRequest.setDeptId(productGuaranteeRequest.getAdminOperate().getDeptId());
        RestResult restResult = new RestResult<>();
        List<ProductGuarantee> productGuarantees = null;
        try {
            ProductGuarantee productGuarantee = BeanUtil.copy(productGuaranteeRequest, ProductGuarantee.class);
            productGuarantee.setDeptId(productGuaranteeRequest.getAdminOperate().getDeptId());
            productGuarantees = productGuaranteeService.query(productGuarantee);
            restResult.setData(productGuarantees);
            restResult.setCode("0000");
            restResult.setMsg("调用成功");
        } catch (Exception e) {
            log.error("9999",e.getLocalizedMessage());
            restResult=RestResult.error("9999",e.getLocalizedMessage());
        }
        return restResult;
    }
}
